import axios from 'axios';
// import { MessageBox, Message } from "ant-design-vue";
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
// 创建axios实例
const service = axios.create({
	baseURL: '/api',
	// 超时
	timeout: 1000 * 60 * 60 * 24,
	withCredentials: true,
});
service.defaults.withCredentials = true;
// request拦截器
service.interceptors.request.use(
	(config) => {
		// document.cookie = "currentOrg=current_org1446784640291962881; power-token=262a0ff9-e2be-4ed2-87fa-280ba9719524; Tenant-Id=1";
		// document.cookie = "currentOrg=current_org1446784640291962881";
		// document.cookie = "power-token=c238f6fa-befc-4470-80e8-c769aac42247";
		// document.cookie = "Tenant-Id=1";
		// config.headers["Cookie"] = "currentOrg=current_org1446784640291962881; power-token=262a0ff9-e2be-4ed2-87fa-280ba9719524; Tenant-Id=1";
		config.headers['Authorization'] ='Bearer 4b0a53a7a1c54a8689d0c8cbe44251f2';
		config.headers['Tenant-Id'] ='1';
		return config;
	},
	(error) => {
		console.log(error);
		Promise.reject(error);
	}
);
// 响应拦截器
service.interceptors.response.use(
	(res: any) => {
		const data = res.data;
		if (data.status === 401) {
			/*  ElMessageBox.alert("登录状态已过期，请重新登录", "系统提示", {
        confirmButtonText: "重新登录",
        callback: () => {
          //  toLogin();
        },
      }); */
		} else if (data.status < 200 || data.status > 300) {
			/* ElMessage({
        message: data.message || "Error",
        type: "error",
        duration: 5 * 1000,
      }); */
			return Promise.reject(new Error(data.message || 'Error'));
		} else if (data.status == 404) {
			/*  ElMessage({
        message: "没有找到服务404",
        type: "error",
        duration: 5 * 1000,
      }); */
			return Promise.reject(new Error(data.message || 'Error'));
		} else {
			return res;
		}
	},
	(error) => {
		const response = error.response;
		if (response) {
			if (response.data.code === 401) {
				/*  ElMessageBox.alert("登录状态已过期，请重新登录", "系统提示", {
          confirmButtonText: "重新登录",
          callback: () => {
            // toLogin();
          },
        }); */
			} else if (response.data.code == 404) {
				/*  ElMessage({
          message: `404 ${response.data.data.error} [${response.data.data.path}]`,
          type: "error",
          duration: 5 * 1000,
        }); */
			} else {
				console.log('err' + error);
				/*  ElMessage({
          message: response.data.message,
          type: "error",
          duration: 5 * 1000,
        }); */
			}
		} else {
			/*   ElMessage({
        message: error.message,
        type: "error",
        duration: 5 * 1000,
      }); */
		}
		return Promise.reject(error);
	}
);
export default service;
